﻿using System;
using System.Linq;
using Styx;
using Styx.Common;
using Styx.CommonBot;
using Styx.TreeSharp;
using Styx.WoWInternals.WoWObjects;
using Action = Styx.TreeSharp.Action;

namespace MIBPriest
{
    public partial class Classname
    {
        #region  BattleStandard

        public Composite UseBattleStandard()
        {
            return new Decorator(
                ret =>
                Me.Combat && Me.HealthPercent < MIBSettings.Instance.HealthstonePercent && Me.CurrentTarget != null &&
                Me.CurrentTarget.CurrentHealth > Me.HealthPercent && Me.CurrentMap.IsBattleground,
                new Action(delegate
                    {
                        if (Me.IsAlliance)
                        {
                            WoWItem bs = Me.BagItems.FirstOrDefault(o => o.Entry == 18606);
                            //18606 Alliance Battle Standard

                            if (!Me.HasAura("Alliance Battle Standard") && bs != null &&
                                bs.CooldownTimeLeft.TotalMilliseconds <= MyLatency)
                            {
                                bs.Use();
                                //Lua.DoString("RunMacroText(\"/5 Used Healthstone\")");
                                //Lua.DoString("RunMacroText(\"/s Used Battle Standard\")");
                                Logging.Write("Use Battle Standard at " + Me.HealthPercent + "%");
                            }
                        }

                        if (Me.IsHorde)
                        {
                            WoWItem bs = Me.BagItems.FirstOrDefault(o => o.Entry == 18607);
                            //18606 Horde Battle Standard

                            if (!Me.HasAura("Horde Battle Standard") && bs != null &&
                                bs.CooldownTimeLeft.TotalMilliseconds <= MyLatency)
                            {
                                bs.Use();
                                //Lua.DoString("RunMacroText(\"/5 Used Healthstone\")");
                                //Lua.DoString("RunMacroText(\"/s Used Battle Standard\")");
                                Logging.Write("Use Battle Standard at " + Me.HealthPercent + "%");
                            }
                        }

                        return RunStatus.Failure;
                    })
                );
        }

        #endregion

        #region  Cascade

        public Composite Cascade()
        {
            return new Decorator(
                ret =>
                UseHalo &&
                MIBSettings.Instance.Cascade &&
                LastCastSpell != "Cascade" &&
                SpellManager.HasSpell("Cascade") &&
                !Me.Mounted &&
                !CastingorGCDL() &&
                GetUnitCascade() &&
                UnitCascade != null &&
                UnitCascade.IsValid &&
                GetSpellCooldown("Cascade").TotalMilliseconds <= MyLatency,
                new Action(delegate
                    {
                        SafelyFacingTarget(UnitCascade);
                        CastSpell("Cascade", UnitCascade);
                    })
                );
        }

        #endregion

        #region  Desperate Prayer

        public Composite DesperatePrayer()
        {
            return new Decorator(
                ret =>
                MIBSettings.Instance.DesperatePrayer > 0 &&
                //LastCastSpell != "Desperate Prayer" &&
                !Me.Mounted &&
                !CastingorGCDL() &&
                Me.Combat &&
                Me.HealthPercent <= MIBSettings.Instance.DesperatePrayer &&
                SpellManager.HasSpell("Desperate Prayer") &&
                SpellManager.Spells["Desperate Prayer"].CooldownTimeLeft.TotalMilliseconds <= MyLatency,
                new Action(delegate
                    {
                        CastSpell("Desperate Prayer", Me);
                        return RunStatus.Failure;
                    })
                );
        }

        #endregion

        #region  Devouring Plague

        public Composite DevouringPlague()
        {
            return new Decorator(
                ret =>
                MIBSettings.Instance.DevouringPlague &&
                LastCastSpell != "Devouring Plague" &&
                !Me.Mounted &&
                CurrentTargetIsValid &&
                CurrentTargetIsEnemy &&
                IsFacing(Me.CurrentTarget) &&
                (Me.CurrentTarget.CurrentHealth > Me.MaxHealth*0.8 ||
                 Me.CurrentTarget.Name.Contains("Dummy")) &&
                !CastingorGCDL() &&
                IsInRange(Me.CurrentTarget, 40) &&
                Me.CurrentTarget.InLineOfSpellSight &&
                Me.GetCurrentPower(WoWPowerType.ShadowOrbs) > 2 &&
                //MyAuraTimeLeft("Devouring Plague", Me.CurrentTarget) < 3000 &&
                SpellManager.HasSpell("Devouring Plague"),
                new Action(delegate
                    {
                        SafelyFacingTarget(Me.CurrentTarget);
                        CastSpell("Devouring Plague", Me.CurrentTarget);
                    })
                );
        }

        #endregion

        #region  Dispersion

        public Composite Dispersion()
        {
            return new Decorator(
                ret =>
                LastCastSpell != "Dispersion" &&
                !Me.Mounted &&
                Me.Combat &&
                !CastingorGCDL() &&
                (Me.ManaPercent <= MIBSettings.Instance.DispersionMana ||
                 Me.HealthPercent <= MIBSettings.Instance.DispersionHP) &&
                SpellManager.HasSpell("Dispersion") &&
                SpellManager.Spells["Dispersion"].CooldownTimeLeft.TotalMilliseconds <= MyLatency,
                new Action(delegate { CastSpell("Dispersion", Me); })
                );
        }

        #endregion

        #region  Fade

        public Composite Fade()
        {
            return new Decorator(
                ret =>
                MIBSettings.Instance.FadeAuto &&
                LastCastSpell != "Fade" &&
                !Me.Mounted &&
                (Me.CurrentMap.IsDungeon || Me.CurrentMap.IsRaid) &&
                CurrentTargetIsValid &&
                CurrentTargetIsEnemy &&
                Me.CurrentTarget.ThreatInfo.RawPercent > 90 &&
                !CastingorGCDL() &&
                IsInRange(Me.CurrentTarget, 10) &&
                SpellManager.HasSpell("Fade"),
                new Action(delegate
                    {
                        CastSpell("Fade", Me);
                        return RunStatus.Failure;
                    })
                );
        }

        #endregion

        #region  Halo

        public Composite Halo()
        {
            return new Decorator(
                ret =>
                UseHalo &&
                MIBSettings.Instance.HaloMin != MIBSettings.Instance.HaloMax &&
                LastCastSpell != "Halo" &&
                !Me.Mounted &&
                CurrentTargetIsValid &&
                CurrentTargetIsEnemy &&
                IsFacing(Me.CurrentTarget) &&
                (Me.CurrentTarget.CurrentHealth > Me.MaxHealth*0.4 ||
                 Me.CurrentTarget.Name.Contains("Dummy")) &&
                !CastingorGCDL() &&
                Me.CurrentTarget.Distance >= MIBSettings.Instance.HaloMin &&
                Me.CurrentTarget.Distance <= MIBSettings.Instance.HaloMax &&
                Me.CurrentTarget.InLineOfSpellSight &&
                IsHaloSafe() &&
                SpellManager.HasSpell("Halo") &&
                SpellManager.Spells["Halo"].CooldownTimeLeft.TotalMilliseconds <= MyLatency,
                new Action(delegate { CastSpell("Halo", Me); })
                );
        }

        #endregion

        #region  Power Word: Fortitude

        public Composite PowerWordFortitude()
        {
            return new Decorator(
                ret =>
                MIBSettings.Instance.PowerWordFortitude &&
                LastCastSpell != "Power Word: Fortitude" &&
                !Me.Mounted &&
                !CastingorGCDL() &&
                !Me.HasAura("Power Word: Fortitude") &&
                !Me.HasAura("Commanding Shout") &&
                SpellManager.HasSpell("Power Word: Fortitude"),
                new Action(delegate { CastSpell("Power Word: Fortitude", Me); })
                );
        }

        #endregion

        #region  Inner Fire

        public Composite InnerFire()
        {
            return new Decorator(
                ret =>
                MIBSettings.Instance.InnerFire &&
                LastCastSpell != "Inner Fire" &&
                !Me.Mounted &&
                !CastingorGCDL() &&
                !Me.HasAura("Inner Fire") &&
                SpellManager.HasSpell("Inner Fire"),
                new Action(delegate { CastSpell("Inner Fire", Me); })
                );
        }

        #endregion

        #region IsInRange

        private static bool IsInRange(WoWUnit target, int range)
        {
            if (target == null)
            {
                return false;
            }

            if (target.DistanceSqr < range*range || SafeTarget.Contains(target.Entry))
            {
                return true;
            }
            return false;
        }

        #endregion

        #region IsFacing

        private static bool IsFacing(WoWUnit target)
        {
            if (target == null)
            {
                return false;
            }

            if (Me.IsFacing(target) || SafeTarget.Contains(target.Entry))
            {
                return true;
            }
            return false;
        }

        #endregion

        #region  Mindbender

        public Composite Mindbender()
        {
            return new Decorator(
                ret =>
                LastCastSpell != "Mindbender" &&
                !Me.Mounted &&
                CurrentTargetIsValid &&
                CurrentTargetIsEnemy &&
                IsFacing(Me.CurrentTarget) &&
                Me.GotAlivePet &&
                !CastingorGCDL() &&
                IsInRange(Me.CurrentTarget, 40) &&
                Me.CurrentTarget.InLineOfSpellSight &&
                SpellManager.HasSpell("Mindbender") &&
                SpellManager.Spells["Mindbender"].CooldownTimeLeft.TotalMilliseconds <= MyLatency,
                new Action(delegate
                    {
                        SafelyFacingTarget(Me.CurrentTarget);
                        CastSpell("Mindbender", Me.CurrentTarget);
                    })
                );
        }

        #endregion

        #region  Mind Blast

        public Composite MindBlast()
        {
            return new Decorator(
                ret =>
                MIBSettings.Instance.MindBlast &&
                LastCastSpell != "Mind Blast" &&
                !Me.Mounted &&
                CurrentTargetIsValid &&
                CurrentTargetIsEnemy &&
                IsFacing(Me.CurrentTarget) &&
                !Me.IsMoving &&
                (Me.CurrentTarget.CurrentHealth > Me.MaxHealth*0.4 ||
                 Me.CurrentTarget.Name.Contains("Dummy")) &&
                !CastingorGCDL() &&
                IsInRange(Me.CurrentTarget, 40) &&
                Me.CurrentTarget.InLineOfSpellSight &&
                SpellManager.HasSpell("Mind Blast") &&
                SpellManager.Spells["Mind Blast"].CooldownTimeLeft.TotalMilliseconds <= MyLatency,
                new Action(delegate
                    {
                        SafelyFacingTarget(Me.CurrentTarget);
                        //CastSpell("Mind Sear", Me.CurrentTarget);
                        CastSpell("Mind Blast", Me.CurrentTarget);
                        LastMindSear = DateTime.Now;
                    })
                );
        }

        #endregion

        #region  Mind Blast Instant

        public Composite MindBlastInstant()
        {
            return new Decorator(
                ret =>
                MIBSettings.Instance.MindBlast &&
                LastCastSpell != "Mind Blast" &&
                !Me.Mounted &&
                CurrentTargetIsValid &&
                CurrentTargetIsEnemy &&
                IsFacing(Me.CurrentTarget) &&
                Me.GetAuraById(124430) != null &&
                (Me.CurrentTarget.CurrentHealth > Me.MaxHealth*0.4 ||
                 Me.CurrentTarget.Name.Contains("Dummy")) &&
                !CastingorGCDL() &&
                IsInRange(Me.CurrentTarget, 40) &&
                Me.CurrentTarget.InLineOfSpellSight &&
                SpellManager.Spells["Mind Blast"].CooldownTimeLeft.TotalMilliseconds <= MyLatency,
                new Action(delegate
                    {
                        SafelyFacingTarget(Me.CurrentTarget);
                        CastSpell("Mind Blast", Me.CurrentTarget);
                    })
                );
        }

        #endregion

        #region  Mind Flay

        //public Composite MindFlay()
        //{
        //    return new Decorator(
        //        ret =>
        //        !Me.Mounted &&
        //        Me.Level > 9 &&
        //        !Me.IsCasting &&
        //        CurrentTargetIsValid &&
        //        CurrentTargetIsEnemy &&
        //        IsFacing(Me.CurrentTarget) &&
        //        !Me.IsMoving &&
        //        (Me.CurrentTarget.CurrentHealth > Me.MaxHealth * 0.4 ||
        //         Me.CurrentTarget.Name.Contains("Dummy")) &&
        //        (!CastingorGCDL() ||
        //         Me.IsCasting && Me.ChanneledCastingSpellId == 15407 &&
        //         Me.CurrentCastTimeLeft.TotalMilliseconds < 1000) &&
        //        IsInRange(Me.CurrentTarget, 40) &&
        //        Me.CurrentTarget.InLineOfSpellSight &&
        //        SpellManager.HasSpell("Mind Flay"),
        //        new Action(delegate
        //            {
        //                SpellManager.Cast(15407, Me.CurrentTarget);
        //                //CastSpell("Mind Flay", Me.CurrentTarget);
        //            })
        //        );
        //}
        public DateTime MindFlayStart;

        public Composite MindFlay()
        {
            return new Decorator(
                ret =>
                MIBSettings.Instance.MindFlay &&
                !Me.Mounted &&
                Me.Level > 9 &&
                CurrentTargetIsValid &&
                CurrentTargetIsEnemy &&
                IsFacing(Me.CurrentTarget) &&
                !Me.IsMoving &&
                MindFlayStart + TimeSpan.FromMilliseconds(MIBSettings.Instance.MindFlayDuration) < DateTime.Now &&
                !CastingorGCDL() &&
                //Me.IsChanneling &&
                //Me.ChanneledCastingSpellId == 15407 &&
                //Me.CurrentChannelTimeLeft.TotalMilliseconds <= 500) &&
                //((!CastingorGCDL() && Me.ChanneledCastingSpellId != 15407) ||
                // (Me.ChanneledCastingSpellId == 15407 && Me.CurrentChannelTimeLeft.TotalMilliseconds <= MyLatency)) &&
                IsInRange(Me.CurrentTarget, 40) &&
                Me.CurrentTarget.InLineOfSpellSight &&
                SpellManager.HasSpell("Mind Flay"),
                new Action(delegate
                    {
                        //if (Me.IsChanneling)
                        //{
                        //    Logging.Write(Me.ChanneledSpell.Name + " " + Me.CurrentChannelTimeLeft.TotalMilliseconds);
                        //}

                        //if (Me.IsCasting)
                        //{
                        //    Logging.Write(Me.CastingSpell.Name + " " + Me.CurrentCastTimeLeft.TotalMilliseconds);
                        //}
                        SafelyFacingTarget(Me.CurrentTarget);
                        CastSpell("Mind Flay", Me.CurrentTarget);
                        MindFlayStart = DateTime.Now;
                    })
                );
        }

        #endregion

        #region  Mind Sear on 3+ Unit

        public static DateTime LastMindSear;

        public Composite MindSear3()
        {
            return new Decorator(
                ret =>
                (UseAoE || !SModeNormal) &&
                !Me.Mounted &&
                !CastingorGCDL() &&
                SpellManager.HasSpell("Mind Sear") &&
                GetUnitMindSear(),
                new Action(delegate
                    {
                        SafelyFacingTarget(UnitMindSear);
                        CastSpell("Mind Sear", UnitMindSear);
                        LastMindSear = DateTime.Now;
                    })
                );
        }

        #endregion

        #region  Mind Sear on 5+ Unit

        public Composite MindSear5()
        {
            return new Decorator(
                ret =>
                (UseAoE || !SModeNormal) &&
                !Me.Mounted &&
                Me.ManaPercent > MIBSettings.Instance.ManaSWP &&
                !CastingorGCDL() &&
                SpellManager.HasSpell("Mind Sear") &&
                GetUnitMindSear5(),
                new Action(delegate
                    {
                        if (Me.CurrentTarget == null)
                        {
                            UnitMindSear.Target();
                        }
                        SafelyFacingTarget(UnitMindSear);
                        CastSpell("Mind Sear", UnitMindSear);
                        LastMindSear = DateTime.Now;
                    })
                );
        }

        #endregion

        #region  Mind Spike

        public Composite MindSpike()
        {
            return new Decorator(
                ret =>
                MIBSettings.Instance.MindSpike &&
                !Me.Mounted &&
                CurrentTargetIsValid &&
                CurrentTargetIsEnemy &&
                IsFacing(Me.CurrentTarget) &&
                !CastingorGCDL() &&
                IsInRange(Me.CurrentTarget, 40) &&
                Me.CurrentTarget.InLineOfSpellSight &&
                Me.HasAura("Surge of Darkness"), // &&
                new Action(delegate
                    {
                        SafelyFacingTarget(Me.CurrentTarget);
                        CastSpell("Mind Spike", Me.CurrentTarget);
                    })
                );
        }

        #endregion

        #region  Power Word: Shield

        public Composite PowerWordShield()
        {
            return new Decorator(
                ret =>
                MIBSettings.Instance.PowerWordShield &&
                Me.ManaPercent > MIBSettings.Instance.ManaSWS &&
                LastCastSpell != "Power Word: Shield" &&
                !Me.Mounted &&
                !CastingorGCDL() &&
                Me.Combat && Me.IsMoving &&
                !Me.HasAura("Weakened Soul") &&
                SpellManager.HasSpell("Body and Soul") &&
                SpellManager.HasSpell("Power Word: Shield"),
                new Action(delegate { CastSpell("Power Word: Shield", Me); })
                );
        }

        #endregion

        #region  Shadow Word: Death

        //public Composite SWD()
        //{
        //    return new Decorator(
        //        ret =>
        //        LastCastSpell != "Shadow Word: Death" &&
        //        !Me.Mounted &&
        //        CurrentTargetIsValid &&
        //        CurrentTargetIsEnemy &&
        //        IsFacing(Me.CurrentTarget) &&
        //        !Me.IsMoving &&
        //        (Me.CurrentTarget.MaxHealth > Me.MaxHealth * 0.8 ||
        //         Me.CurrentTarget.Name.Contains("Dummy")) &&
        //        Me.CurrentTarget.HealthPercent <= 20 &&
        //        !CastingorGCDL() &&
        //        IsInRange(Me.CurrentTarget, 40) &&
        //        Me.CurrentTarget.InLineOfSpellSight &&
        //        SpellManager.HasSpell("Shadow Word: Death") &&
        //        SpellManager.Spells["Shadow Word: Death"].CooldownTimeLeft.TotalMilliseconds <= MyLatency &&
        //        GetSpellCooldown("Shadow Word: Death").TotalMilliseconds <= MyLatency,
        //        new Action(delegate { CastSpell("Shadow Word: Death", Me.CurrentTarget); })
        //        );
        //}

        public Composite SWD()
        {
            return new Decorator(
                ret =>
                MIBSettings.Instance.SWD &&
                !Me.Mounted &&
                CurrentTargetIsValid &&
                CurrentTargetIsEnemy &&
                (Me.CurrentTarget.MaxHealth > Me.MaxHealth*0.8 ||
                 Me.CurrentTarget.Name.Contains("Dummy")) &&
                Me.CurrentTarget.HealthPercent <= 20 &&
                Me.HealthPercent >= 20 &&
                !CastingorGCDL() &&
                IsInRange(Me.CurrentTarget, 40) &&
                SpellManager.HasSpell("Shadow Word: Death") &&
                GetSpellCooldown("Shadow Word: Death").TotalMilliseconds <= MyLatency,
                new Action(delegate
                    {
                        SafelyFacingTarget(Me.CurrentTarget);
                        CastSpell("Shadow Word: Death", Me.CurrentTarget);
                    })
                );
        }

        #endregion

        #region  Shadow Word: Death Off Target

        public Composite SWDOT()
        {
            return new Decorator(
                ret =>
                MIBSettings.Instance.SWD &&
                LastCastSpell != "Shadow Word: Death" &&
                !Me.Mounted &&
                !CastingorGCDL() &&
                SpellManager.HasSpell("Shadow Word: Death") &&
                SpellManager.Spells["Shadow Word: Death"].CooldownTimeLeft.TotalMilliseconds <= MyLatency &&
                GetSpellCooldown("Shadow Word: Death").TotalMilliseconds <= MyLatency &&
                GetUnitSWD(),
                new Action(delegate
                    {
                        if (Me.CurrentTarget == null)
                        {
                            UnitSWD.Target();
                        }
                        SafelyFacingTarget(UnitSWD);
                        CastSpell("Shadow Word: Death", UnitSWD);
                    })
                );
        }

        #endregion

        #region  Shadow Word: Pain

        public Composite ShadowWordPain()
        {
            return new Decorator(
                ret =>
                !Me.Mounted &&
                CurrentTargetIsValid &&
                CurrentTargetIsEnemy &&
                !CastingorGCDL() &&
                IsInRange(Me.CurrentTarget, 40) &&
                Me.CurrentTarget.InLineOfSpellSight &&
                MyAuraTimeLeft("Shadow Word: Pain", Me.CurrentTarget) <= MIBSettings.Instance.SWPRefresh &&
                SpellManager.HasSpell("Shadow Word: Pain"),
                new Action(delegate { CastSpell("Shadow Word: Pain", Me.CurrentTarget); })
                );
        }

        #endregion

        #region  Shadow Word: Pain Spam While Moving

        public Composite ShadowWordPainMoving()
        {
            return new Decorator(
                ret =>
                Me.ManaPercent > MIBSettings.Instance.ManaSWP &&
                !Me.Mounted &&
                Me.IsMoving &&
                CurrentTargetIsValid &&
                CurrentTargetIsEnemy &&
                !CastingorGCDL() &&
                GetUnitSWPMoving() &&
                SpellManager.HasSpell("Shadow Word: Pain"),
                new Action(delegate
                    {
                        //if (Me.CurrentTarget == null)
                        //{
                        //    UnitSWPMoving.Target();
                        //}
                        CastSpell("Shadow Word: Pain", UnitSWPMoving);
                    })
                );
        }

        #endregion

        #region  Shadow Word: Pain Multi Target

        public Composite ShadowWordPainMulti()
        {
            return new Decorator(
                ret =>
                (UseAoE || !SModeNormal) &&
                Me.ManaPercent > MIBSettings.Instance.ManaSWP &&
                !Me.Mounted &&
                Me.ManaPercent > 50 &&
                !CastingorGCDL() &&
                SpellManager.HasSpell("Shadow Word: Pain") &&
                GetUnitSWP(),
                new Action(delegate
                    {
                        //if (Me.CurrentTarget == null)
                        //{
                        //    UnitSWP.Target();
                        //}
                        CastSpell("Shadow Word: Pain", UnitSWP);
                    })
                );
        }

        #endregion

        #region  Shadowfiend

        public Composite Shadowfiend()
        {
            return new Decorator(
                ret =>
                MIBSettings.Instance.ShadowfiendAuto &&
                LastCastSpell != "Shadowfiend" &&
                !Me.Mounted &&
                CurrentTargetIsValid &&
                CurrentTargetIsEnemy &&
                IsFacing(Me.CurrentTarget) &&
                (Me.CurrentTarget.CurrentHealth > Me.CurrentHealth*0.8 &&
                 Me.CurrentTarget.MaxHealth > Me.MaxHealth*5 &&
                 Me.CurrentTarget.ThreatInfo.RawPercent < 70 ||
                 Me.CurrentTarget.Name.Contains("Dummy")) &&
                !CastingorGCDL() &&
                IsInRange(Me.CurrentTarget, 40) &&
                Me.CurrentTarget.InLineOfSpellSight &&
                SpellManager.HasSpell("Shadowfiend") &&
                SpellManager.Spells["Shadowfiend"].CooldownTimeLeft.TotalMilliseconds <= MyLatency,
                new Action(delegate
                    {
                        SafelyFacingTarget(Me.CurrentTarget);
                        CastSpell("Shadowfiend", Me.CurrentTarget);
                    })
                );
        }

        #endregion

        #region  Shadowform

        public Composite Shadowform()
        {
            return new Decorator(
                ret =>
                MIBSettings.Instance.Shadowform &&
                LastCastSpell != "Shadowform" &&
                !Me.Mounted &&
                !CastingorGCDL() &&
                !Me.HasAura("Shadowform") &&
                SpellManager.HasSpell("Shadowform"),
                new Action(delegate { CastSpell("Shadowform", Me); })
                );
        }

        #endregion

        #region UseHealthstone

        public Composite UseHealthstone()
        {
            return new Decorator(
                ret =>
                MIBSettings.Instance.HealthstonePercent > 0 &&
                Me.Combat && Me.HealthPercent < MIBSettings.Instance.HealthstonePercent && Me.CurrentTarget != null &&
                Me.CurrentTarget.CurrentHealth > Me.HealthPercent,
                new Action(delegate
                    {
                        WoWItem hs = Me.BagItems.FirstOrDefault(o => o.Entry == 5512); //5512 Healthstone
                        if (hs != null && hs.CooldownTimeLeft.TotalMilliseconds <= MyLatency)
                        {
                            hs.Use();
                            //Lua.DoString("RunMacroText(\"/s Used Healthstone\")");
                            Logging.Write("Use Healthstone at " + Me.HealthPercent + "%");
                        }
                        return RunStatus.Failure;
                    })
                );
        }

        #endregion

        #region   Vampiric Embrace

        public Composite VampiricEmbrace()
        {
            return new Decorator(
                ret =>
                MIBSettings.Instance.VampiricEmbracePercent > 0 &&
                LastCastSpell != "Vampiric Embrace" &&
                !Me.Mounted &&
                CurrentTargetIsValid &&
                CurrentTargetIsEnemy &&
                Me.HealthPercent <= MIBSettings.Instance.VampiricEmbracePercent &&
                !CastingorGCDL() &&
                Me.CurrentTarget.InLineOfSpellSight &&
                SpellManager.HasSpell("Vampiric Embrace") &&
                SpellManager.Spells["Vampiric Embrace"].CooldownTimeLeft.TotalMilliseconds <= MyLatency,
                new Action(delegate
                    {
                        //SpellManager.StopCasting();
                        CastSpell("Vampiric Embrace", Me);
                        return RunStatus.Failure;
                    })
                );
        }

        #endregion

        #region  Vampiric Touch

        public Composite VampiricTouch()
        {
            return new Decorator(
                ret =>
                !Me.Mounted &&
                CurrentTargetIsValid &&
                CurrentTargetIsEnemy &&
                IsFacing(Me.CurrentTarget) &&
                !Me.IsMoving &&
                (LastVT.AddSeconds(3) < DateTime.Now || Me.CurrentTarget != LastVTTarget) &&
                (Me.CurrentTarget.CurrentHealth > Me.MaxHealth*0.4 ||
                 Me.CurrentTarget.Name.Contains("Dummy")) &&
                !CastingorGCDL() &&
                IsInRange(Me.CurrentTarget, 40) &&
                Me.CurrentTarget.InLineOfSpellSight &&
                SpellManager.HasSpell("Vampiric Touch") &&
                MyAuraTimeLeft("Vampiric Touch", Me.CurrentTarget) < MIBSettings.Instance.VampiricTouchRefresh,
                new Action(delegate
                    {
                        //SpellManager.StopCasting();
                        SafelyFacingTarget(Me.CurrentTarget);
                        CastSpell("Vampiric Touch", Me.CurrentTarget);
                        LastVT = DateTime.Now;
                        LastVTTarget = Me.CurrentTarget;
                    })
                );
        }

        #endregion

        #region  Vampiric Touch Multi Target

        public Composite VampiricTouchMulti()
        {
            return new Decorator(
                ret =>
                (UseAoE || !SModeNormal) &&
                !Me.Mounted &&
                !Me.IsMoving &&
                LastVT.AddSeconds(3) < DateTime.Now &&
                !CastingorGCDL() &&
                SpellManager.HasSpell("Vampiric Touch") &&
                GetUnitVT() &&
                (LastVT.AddSeconds(3) < DateTime.Now || UnitVT != LastVTTarget),
                new Action(delegate
                    {
                        if (Me.CurrentTarget == null)
                        {
                            UnitVT.Target();
                        }
                        SafelyFacingTarget(UnitVT);
                        CastSpell("Vampiric Touch", UnitVT);
                        LastVT = DateTime.Now;
                        LastVTTarget = UnitVT;
                    })
                );
        }

        #endregion
    }
}